import Vue from 'vue'
import App from './App.vue'
import VueRouter from "vue-router";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from "@/router/router";
import axios from "axios";
import login from "@/vuejs/login";


Vue.prototype.GLOBAL = login
Vue.use(ElementUI)
Vue.config.productionTip = false

Vue.use(VueRouter)
Vue.prototype.$axios = axios.create({
  headers: { 'content-type' : 'application/x-www-form-urlencoded'}
});

// 请求拦截器
axios.interceptors.request.use(config=>{
    console.log("token"+window.localStorage.getItem("token"))
      // 正则表达式 判断路径是否为登录注册的路径
      let url = /^(\/login.do.*)$/
    let url1 = /^(\/msg\/query)$/
        console.log(!url.test(config.url));
      if (config.url==="/login.do/logout"){
          console.log("token"+window.localStorage.getItem("token"))
          let token = window.localStorage.getItem("token")
          config.headers.token = token;
      }
      if ( !url.test(config.url) && "/msg/query"!==config.url){
        let token = window.localStorage.getItem("token")
        if (token !== null && token.length!==0){
          config.headers.token = token;
        }else {
          //   没有token就是没有登录
          alert("未登录无法操作请登录")
            // this.$router.push({
            //     path:"/"
            // })
        }
      }
      return config;

    }

)
// 响应拦截器
axios.interceptors.response.use(data=>{
      if (data.data.code===1002){
        //   登录过期删除信息
        window.localStorage.removeItem("token");
        window.localStorage.removeItem("UserName")
          window.localStorage.removeItem("ID")
          window.localStorage.removeItem("icon")
          Vue.prototype.$message(data.data.msg+"请返回登录")
          // 不返回了
          // window.location.href='/'
      //     登录失败情况 还是不做处理了把
      }else if(data.data.code===1003){
          window.localStorage.removeItem("token");
          window.localStorage.removeItem("UserName")
          window.localStorage.removeItem("ID")
          window.localStorage.removeItem("icon")

      }
      return data;
    }
)

const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// 重写原型上的push方法，统一处理错误信息
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
VueRouter.prototype.replace= function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}
axios.defaults.baseURL="/xxyf/"
// axios.defaults.withCredentials=true

// axios.defaults.baseURL="http://localhost:8080/mvc1"
// axios.defaults.baseURL="http://xxyf.fgimaxl2.vipnps.vip/mvc1"
new Vue({
  //用render来创建vue
  render: h => h(App),
  router:router
}).$mount('#app')
